package com.no1.dao;

import com.no1.entity.Function;
import com.no1.utils.DBHelper;

import java.util.*;

public class FunctionDao {
    //根据用户角色获取功能
    public List<Function> getFun(int id){
        List<Function> result = new ArrayList<>();
        String sql = "SELECT a.* FROM functions a,righrt b WHERE a.`fid` = b.`fid` AND b.`rid`=? ";
        List<Integer> params = new ArrayList<>();
        params.add(id);
        List<Map<String,Object>> values = DBHelper.executeQuery(sql,params);
        for(Map<String,Object> va:values){
            result.add(getFunction(va));
        }
        return result;
    }
    //获取所有功能
    public List<Function> getAllFun(){
        List<Function> result =new ArrayList<>();
        String sql = "select * from functions";
        List<Map<String,Object>> values = DBHelper.executeQuery(sql,null);
        for (Map<String,Object> va:values){
            result.add(getFunction(va));
        }
        return result;
    }
    private Function getFunction(Map<String,Object> va){
        Function f = new Function();
        f.setFid((Integer) va.get("fid"));
        f.setFname((String) va.get("fname"));
        f.setLianjie((String) va.get("lianjie"));
        f.setParentid((Integer) va.get("parentid"));
        f.setState((Integer) va.get("state"));
        return f;
    }
}
